import { all, call, put, takeLatest } from 'redux-saga/effects'
import api from '@API'
import {
  SAVE_WECHAT_STEP_RECORDS,
  GET_TODAY_STEP,
  GET_TODAY_PAIN_LOG,
  GET_LASTEST_EXPIRED_PLAN,
  GET_LOGS_BY_DATE_BUCKET, DELETE_LOG, GET_MONTH_LOG
} from '@ACTIONS/dataCenter'
import {
  DataCenterActionITF,
  DataCenterDeleteLoggActionITF,
  DataCenterGetLogsActionITF,
  DataCenterGetMonthLogActionITF
} from '@REDUXITF/dataCenter'
import { PainLogITF } from '@INTERFACES/PainLog'
import i18n from '@I18N'
import { showToast } from '@UTILS/common'

const saveWeChatStepRecords = function* saveWeChatStepRecords(action: DataCenterActionITF) {
  try {
    const { records = [] } = action
    const haveData = records.length > 0
    if (!haveData) return
    const data = yield call(api.saveWeChatStepRecords, { Records: records })
    yield put({ type: SAVE_WECHAT_STEP_RECORDS.SUCCESS, data: data.TodayRecord })
  } catch (error) {
    yield put({ type: SAVE_WECHAT_STEP_RECORDS.FAILURE, error })
  }
}

const getTodayWeChatStepRecord = function* getTodayWeChatStepRecord() {
  try {
    const data = yield call(api.getTodayWeChatStepRecord, {})
    yield put({ type: GET_TODAY_STEP.SUCCESS, data: data.TodayRecord })
  } catch (error) {
    yield put({ type: GET_TODAY_STEP.FAILURE, error })
  }
}
const getTodayPainLogSummary = function* getTodayPainLogSummary() {
  try {
    const data = yield call(api.getTodayPainLogSummary, {})
    yield put({ type: GET_TODAY_PAIN_LOG.SUCCESS, data })
  } catch (error) {
    yield put({ type: GET_TODAY_PAIN_LOG.FAILURE, error })
  }
}

const getMyPainLogsByDateBucket = function* getMyPainLogsByDateBucket(action: DataCenterGetLogsActionITF) {
  try {
    const { dateBucket } = action
    const data = yield call(api.getMyPainLogsByDateBucket, { dateBucket })
    yield put({ type: GET_LOGS_BY_DATE_BUCKET.SUCCESS, data: data?.PainLogs || [] })
  } catch (error) {
    yield put({ type: GET_LOGS_BY_DATE_BUCKET.FAILURE, error })
  }
}
const deleteMyPainLog = function* deleteMyPainLog(action: DataCenterDeleteLoggActionITF) {
  try {
    const { logId, pId, bucket } = action
    yield call(api.deleteMyPainLog, { PainLogId: logId })
    yield put({ type: DELETE_LOG.SUCCESS, logId })
    yield put({ type: GET_MONTH_LOG.REQUEST, pId, bucket })
    showToast('已删除')
  } catch (error) {
    yield put({ type: DELETE_LOG.FAILURE, error })
  }
}

const getPatientMonthPainLog = function* getPatientMonthPainLog(action: DataCenterGetMonthLogActionITF) {
  try {
    const { bucket, pId } = action
    const result: { PainLogs : PainLogITF[]} = yield call(api.getPatientMonthPainLog, { bucket, pId })

    const data = result.PainLogs.map(item => {
      const { DateBucket, PainSite, OverallRating, HeadPain, Menstruation } = item
      const isNoPain = OverallRating === 0
      let text = ''
      if (isNoPain) {
        text = i18n.dataCenter.noPain()
      } else {
        const bodyText = PainSite?.zh ? `${PainSite?.zh}痛` : ''
        text = HeadPain ? i18n.common.headache() : bodyText
      }
      return ({ value: DateBucket, text, icon: Menstruation, isGrey: isNoPain })
    })
    yield put({ type: GET_MONTH_LOG.SUCCESS, data })
  } catch (error) {
    yield put({ type: GET_MONTH_LOG.FAILURE, error })
  }
}
const getMyLastestExpiredPainManagement = function* getMyLastestExpiredPainManagement() {
  try {
    const data = yield call(api.getMyLastestExpiredPainManagement, {})
    yield put({ type: GET_LASTEST_EXPIRED_PLAN.SUCCESS, data })
  } catch (error) {
    yield put({ type: GET_LASTEST_EXPIRED_PLAN.FAILURE, error })
  }
}

export default function* rootSaga(): Generator {
  yield all([
    takeLatest(GET_TODAY_STEP.REQUEST, getTodayWeChatStepRecord),
    takeLatest(SAVE_WECHAT_STEP_RECORDS.REQUEST, saveWeChatStepRecords),
    takeLatest(GET_TODAY_PAIN_LOG.REQUEST, getTodayPainLogSummary),
    takeLatest(GET_LASTEST_EXPIRED_PLAN.REQUEST, getMyLastestExpiredPainManagement),
    takeLatest(GET_LOGS_BY_DATE_BUCKET.REQUEST, getMyPainLogsByDateBucket),
    takeLatest(DELETE_LOG.REQUEST, deleteMyPainLog),
    takeLatest(GET_MONTH_LOG.REQUEST, getPatientMonthPainLog),
  ])
}
